<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Crafty - Crafty.audio</title>
	<link type="text/css" rel="stylesheet" href="/craftyjs-site.css" />

	<link href='http://fonts.googleapis.com/css?family=Arvo:regular,bold' rel='stylesheet' 	type='text/css'>
	<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600' rel='stylesheet' type='text/css'>
	<link rel="shortcut icon" href="/favicon.ico">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
	<link rel="stylesheet" href="/github.css"/>
	<script type="text/javascript">

		var _gaq = _gaq || [];
		_gaq.push(['_setAccount', 'UA-23935213-2']);
		_gaq.push(['_trackPageview']);

		(function () {
			var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
		})();

	</script>
</head>
<body>
	<div id = "header-background"> </div>
	<div id = "page-frame">
	<div id="header">
		<nav class="navbar">
  			<div class="container-fluid">
  				<div class="navbar-header">
  					<a class="navbar-brand" href="/"> <img class="logo" src="/images/text-logo.png" /> </a>
  					
				</div>
				<div class="collapse navbar-collapse navbar-right" id="bs-example-navbar-collapse-1">
					<ul class="nav navbar-nav">
						<li><a href="/">Home</a></li>
						<li><a href="/getting-started/">Getting started</a></li>
						<li><a href="/documentation/">Documentation</a></li>
						<li><a href="/api/">API</a></li>
						<li><a href="https://groups.google.com/forum/#!forum/craftyjs">Forum</a></li>
						<li><a href="/components/">Components</a></li>
						<li class="emph"><a href="/#install">Download</a></li>
					</ul>
				</div>
			</div>
		</nav>
	</div>


	<div id="main">
		<div id="content" class="container">
			



<div id="docs"><div class="toc-holder" id="doc-nav"><ul id="doc-level-one"><li><a href="events.html">List of Events</a></li><li class="category">Core<ul class="category-list"><li><a href="Crafty.html">Crafty</a></li><li><a href="Crafty Core.html">Crafty Core</a></li><li><a href="Crafty-bind.html">Crafty.bind()</a></li><li><a href="Crafty-c.html">Crafty.c()</a></li><li><a href="Crafty-clone.html">Crafty.clone()</a></li><li><a href="Crafty-defineField.html">Crafty.defineField()</a></li><li><a href="Crafty-e.html">Crafty.e()</a></li><li><a href="Crafty-extend.html">Crafty.extend()</a></li><li><a href="Crafty-frame.html">Crafty.frame()</a></li><li><a href="Crafty-getVersion.html">Crafty.getVersion()</a></li><li><a href="Crafty-init.html">Crafty.init()</a></li><li><a href="Crafty-isPaused.html">Crafty.isPaused()</a></li><li><a href="Crafty-one.html">Crafty.one()</a></li><li><a href="Crafty-pause.html">Crafty.pause()</a></li><li><a href="Crafty-s.html">Crafty.s()</a></li><li><a href="Crafty-settings.html">Crafty.settings</a></li><li><a href="Crafty-stage.html">Crafty.stage</a></li><li><a href="Crafty-stop.html">Crafty.stop()</a></li><li><a href="Crafty-support.html">Crafty.support</a></li><li><a href="Crafty-trigger.html">Crafty.trigger()</a></li><li><a href="Crafty-unbind.html">Crafty.unbind()</a></li><li><a href="Crafty-uniqueBind.html">Crafty.uniqueBind()</a></li></ul></li><li class="category">2D<ul class="category-list"><li><a href="2D.html">2D</a></li><li><a href="AngularMotion.html">AngularMotion</a></li><li><a href="Collision.html">Collision</a></li><li><a href="Crafty-circle.html">Crafty.circle</a></li><li><a href="Crafty-diamondIso.html">Crafty.diamondIso</a></li><li><a href="Crafty-HashMap.html">Crafty.HashMap</a></li><li><a href="Crafty-isometric.html">Crafty.isometric</a></li><li><a href="Crafty-map.html">Crafty.map</a></li><li><a href="Crafty-math-Matrix2D.html">Crafty.math.Matrix2D</a></li><li><a href="Crafty-math-Vector2D.html">Crafty.math.Vector2D</a></li><li><a href="Crafty-polygon.html">Crafty.polygon</a></li><li><a href="Crafty-raycast.html">Crafty.raycast()</a></li><li><a href="Crafty-rectManager.html">Crafty.rectManager</a></li><li><a href="Gravity.html">Gravity</a></li><li><a href="GroundAttacher.html">GroundAttacher</a></li><li><a href="Motion.html">Motion</a></li><li><a href="Supportable.html">Supportable</a></li></ul></li><li class="category">Animation<ul class="category-list"><li><a href="Crafty-easing.html">Crafty.easing</a></li><li><a href="SpriteAnimation.html">SpriteAnimation</a></li><li><a href="Tween.html">Tween</a></li></ul></li><li class="category">Assets<ul class="category-list"><li><a href="Crafty-asset.html">Crafty.asset()</a></li><li><a href="Crafty-assets.html">Crafty.assets</a></li><li><a href="Crafty-imageWhitelist.html">Crafty.imageWhitelist()</a></li><li><a href="Crafty-load.html">Crafty.load()</a></li><li><a href="Crafty-paths.html">Crafty.paths()</a></li><li><a href="Crafty-removeAssets.html">Crafty.removeAssets()</a></li></ul></li><li class="category">Audio<ul class="category-list"><li><a href="Crafty-audio.html">Crafty.audio</a></li></ul></li><li class="category">Controls<ul class="category-list"><li><a href="Controllable.html">Controllable</a></li><li><a href="Controls.html">Controls</a></li><li><a href="Draggable.html">Draggable</a></li><li><a href="Fourway.html">Fourway</a></li><li><a href="Jumper.html">Jumper</a></li><li><a href="Multiway.html">Multiway</a></li><li><a href="Twoway.html">Twoway</a></li></ul></li><li class="category">Debug<ul class="category-list"><li><a href="Crafty-error.html">Crafty.error()</a></li><li><a href="Crafty-log.html">Crafty.log()</a></li><li><a href="DebugCanvas.html">DebugCanvas</a></li><li><a href="DebugPolygon.html">DebugPolygon</a></li><li><a href="DebugRectangle.html">DebugRectangle</a></li><li><a href="SolidAreaMap.html">SolidAreaMap</a></li><li><a href="SolidHitBox.html">SolidHitBox</a></li><li><a href="VisibleMBR.html">VisibleMBR</a></li><li><a href="WiredAreaMap.html">WiredAreaMap</a></li><li><a href="WiredHitBox.html">WiredHitBox</a></li></ul></li><li class="category">Events<ul class="category-list"><li><a href="Crafty-addEvent.html">Crafty.addEvent()</a></li><li><a href="Crafty-bind.html">Crafty.bind()</a></li><li><a href="Crafty-one.html">Crafty.one()</a></li><li><a href="Crafty-removeEvent.html">Crafty.removeEvent()</a></li><li><a href="Crafty-trigger.html">Crafty.trigger()</a></li><li><a href="Crafty-unbind.html">Crafty.unbind()</a></li><li><a href="Crafty-uniqueBind.html">Crafty.uniqueBind()</a></li></ul></li><li class="category">Game Loop<ul class="category-list"><li><a href="Crafty-timer.html">Crafty.timer</a></li></ul></li><li class="category">Graphics<ul class="category-list"><li><a href="Canvas.html">Canvas</a></li><li><a href="CanvasLayer.html">CanvasLayer</a></li><li><a href="Color.html">Color</a></li><li><a href="Crafty-assignColor.html">Crafty.assignColor()</a></li><li><a href="Crafty-background.html">Crafty.background()</a></li><li><a href="Crafty-createLayer.html">Crafty.createLayer()</a></li><li><a href="Crafty-defaultShader.html">Crafty.defaultShader()</a></li><li><a href="Crafty-domHelper.html">Crafty.domHelper</a></li><li><a href="Crafty-pixelart.html">Crafty.pixelart()</a></li><li><a href="Crafty-sprite.html">Crafty.sprite()</a></li><li><a href="Crafty-WebGLShader.html">Crafty.WebGLShader()</a></li><li><a href="DOM.html">DOM</a></li><li><a href="DomLayer.html">DomLayer</a></li><li><a href="HTML.html">HTML</a></li><li><a href="Image.html">Image</a></li><li><a href="Particles.html">Particles</a></li><li><a href="Renderable.html">Renderable</a></li><li><a href="Sprite.html">Sprite</a></li><li><a href="Text.html">Text</a></li><li><a href="WebGL.html">WebGL</a></li><li><a href="WebGLLayer.html">WebGLLayer</a></li></ul></li><li class="category">Input<ul class="category-list"><li><a href="AreaMap.html">AreaMap</a></li><li><a href="Button.html">Button</a></li><li><a href="Crafty-findPointerEventTargetByComponent.html">Crafty.findPointerEventTargetByComponent()</a></li><li><a href="Crafty-keyboardDispatch.html">Crafty.keyboardDispatch()</a></li><li><a href="Crafty-keydown.html">Crafty.keydown</a></li><li><a href="Crafty-keys.html">Crafty.keys</a></li><li><a href="Crafty-lastEvent.html">Crafty.lastEvent</a></li><li><a href="Crafty-mouseButtons.html">Crafty.mouseButtons</a></li><li><a href="Crafty-mouseDispatch.html">Crafty.mouseDispatch()</a></li><li><a href="Crafty-mouseWheelDispatch.html">Crafty.mouseWheelDispatch()</a></li><li><a href="Crafty-multitouch.html">Crafty.multitouch()</a></li><li><a href="Crafty-selected.html">Crafty.selected</a></li><li><a href="Crafty-touchDispatch.html">Crafty.touchDispatch()</a></li><li><a href="Keyboard.html">Keyboard</a></li><li><a href="Mouse.html">Mouse</a></li><li><a href="MouseDrag.html">MouseDrag</a></li><li><a href="Touch.html">Touch</a></li></ul></li><li class="category">Misc<ul class="category-list"><li><a href="Crafty-addEvent.html">Crafty.addEvent()</a></li><li><a href="Crafty-device.html">Crafty.device</a></li><li><a href="Crafty-removeEvent.html">Crafty.removeEvent()</a></li><li><a href="Crafty-support.html">Crafty.support</a></li></ul></li><li class="category">Model<ul class="category-list"><li><a href="Model.html">Model</a></li></ul></li><li class="category">Scenes<ul class="category-list"><li><a href="Crafty-scene.html">Crafty.scene()</a></li></ul></li><li class="category">Stage<ul class="category-list"><li><a href="Crafty-background.html">Crafty.background()</a></li><li><a href="Crafty-scene.html">Crafty.scene()</a></li><li><a href="Crafty-viewport.html">Crafty.viewport</a></li></ul></li><li class="category">Utilities<ul class="category-list"><li><a href="Crafty-math.html">Crafty.math</a></li><li><a href="Delay.html">Delay</a></li><li><a href="Storage.html">Storage</a></li></ul></li></ul></div><div id="doc-content" class="doc-page-holder"><div class="doc-page"><h1>Crafty.audio <span></span></h1><div class="doclet" id="Crafty-audio"><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L5-L17">src/sound/sound.js#L5-L17</a></div><span class="markdown"><p>Add sound files and play them. Chooses best format for browser support.
Due to the nature of HTML5 audio, three types of audio files will be
required for cross-browser capabilities. These formats are MP3, Ogg and WAV.
When sound was not muted on before pause, sound will be unmuted after unpause.
When sound is muted Crafty.pause() does not have any effect on sound</p>
<p>The maximum number of sounds that can be played simultaneously is defined by Crafty.audio.maxChannels.  The default value is 7.</p>
</span></div><div><h4>Methods</h4><ul class="page-toc"><li><a href="#Crafty-audio-add">.add()</a></li><li><a href="#Crafty-audio-create">.create()</a></li><li><a href="#Crafty-audio-isPlaying">.isPlaying()</a></li><li><a href="#Crafty-audio-mute">.mute()</a></li><li><a href="#Crafty-audio-pause">.pause()</a></li><li><a href="#Crafty-audio-play">.play()</a></li><li><a href="#Crafty-audio-remove">.remove()</a></li><li><a href="#Crafty-audio-setChannels">.setChannels()</a></li><li><a href="#Crafty-audio-stop">.stop()</a></li><li><a href="#Crafty-audio-supports">.supports()</a></li><li><a href="#Crafty-audio-toggleMute">.toggleMute()</a></li><li><a href="#Crafty-audio-togglePause">.togglePause()</a></li><li><a href="#Crafty-audio-unmute">.unmute()</a></li><li><a href="#Crafty-audio-unpause">.unpause()</a></li></ul><div class="doclet" id="Crafty-audio-add"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.add()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L121-L164">src/sound/sound.js#L121-L164</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.add</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">id</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">String </span><span class="sig-noun">url</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> id </dt><dd><span class="markdown"><p>A string to refer to sounds</p>
</span></dd></dl><dl class="parameter"><dt> url </dt><dd><span class="markdown"><p>A string pointing to the sound file</p>
</span></dd></dl></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.add</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">id</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">Array </span><span class="sig-noun">urls</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> urls </dt><dd><span class="markdown"><p>Array of urls pointing to different format of the same sound, selecting the first that is playable</p>
</span></dd></dl></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.add</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">Object </span><span class="sig-noun">map</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> map </dt><dd><span class="markdown"><p>key-value pairs where the key is the <code>id</code> and the value is either a <code>url</code> or <code>urls</code></p>
</span></dd></dl></div><span class="markdown"><p>Loads a sound to be played. Due to the nature of HTML5 audio,
three types of audio files will be required for cross-browser capabilities.
These formats are MP3, Ogg and WAV.</p>
<p>Passing an array of URLs will determine which format the browser can play and select it over any other.</p>
<p>Accepts an object where the key is the audio name and
either a URL or an Array of URLs (to determine which type to use).</p>
<p>The ID you use will be how you refer to that sound when using <code>Crafty.audio.play</code>.</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs "><span class="hljs-comment">//adding audio from an object</span>
Crafty.audio.add({
  <span class="hljs-attr">shoot</span>: [<span class="hljs-string">"sounds/shoot.wav"</span>,
          <span class="hljs-string">"sounds/shoot.mp3"</span>,
          <span class="hljs-string">"sounds/shoot.ogg"</span>]
});

<span class="hljs-comment">//adding a single sound</span>
Crafty.audio.add(<span class="hljs-string">"walk"</span>, [
<span class="hljs-string">"sounds/walk.mp3"</span>,
<span class="hljs-string">"sounds/walk.ogg"</span>,
<span class="hljs-string">"sounds/walk.wav"</span>
]);

<span class="hljs-comment">//only one format</span>
Crafty.audio.add(<span class="hljs-string">"jump"</span>, <span class="hljs-string">"sounds/jump.mp3"</span>);</code></pre></span></div></div><div class="doclet" id="Crafty-audio-create"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.create()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L83-L96">src/sound/sound.js#L83-L96</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.create</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">id</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">String </span><span class="sig-noun">url</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> id </dt><dd><span class="markdown"><p>A string to refer to sounds</p>
</span></dd></dl><dl class="parameter"><dt> url </dt><dd><span class="markdown"><p>A string pointing to the sound file</p>
</span></dd></dl></div><span class="markdown"><p>Creates an audio asset with the given id and resource.  <code>Crafty.audio.add</code> is a more flexible interface that allows cross-browser compatibility.</p>
<p>If the sound file extension is not supported, returns false; otherwise, returns the audio asset.</p>
</span></div><div class="doclet" id="Crafty-audio-isPlaying"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.isPlaying()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L557-L573">src/sound/sound.js#L557-L573</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">Boolean </span><span class="sig-noun">Crafty.audio.isPlaying</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">string </span><span class="sig-noun">ID</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> {string} id </dt><dd><span class="markdown"><p>The id of the audio object</p>
</span></dd></dl><dl class="parameter returns"><dt class="returns"> [Returns] </dt><dd><span class="markdown"><p>a Boolean indicating whether the audio is playing or not</p>
</span></dd></dl></div><span class="markdown"><p>Check if audio with the given ID is playing or not (on at least one channel).</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs "><span class="hljs-keyword">var</span> isPlaying = Crafty.audio.isPlaying(<span class="hljs-string">'music'</span>);</code></pre></span></div></div><div class="doclet" id="Crafty-audio-mute"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.mute()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L435-L448">src/sound/sound.js#L435-L448</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.mute</span></span><span class="sig-seperator">()</span></code></div><span class="markdown"><p>Mute every Audio instance that is playing.</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs ">Crafty.audio.mute();</code></pre></span></div></div><div class="doclet" id="Crafty-audio-pause"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.pause()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L470-L485">src/sound/sound.js#L470-L485</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.pause</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">string </span><span class="sig-noun">ID</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> {string} id </dt><dd><span class="markdown"><p>The id of the audio object to pause</p>
</span></dd></dl></div><span class="markdown"><p>Pause the Audio instance specified by id param.</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs ">Crafty.audio.pause(<span class="hljs-string">'music'</span>);</code></pre></span></div></div><div class="doclet" id="Crafty-audio-play"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.play()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L198-L224">src/sound/sound.js#L198-L224</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.play</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">id</span></span><span class="sig-seperator">)</span></code></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.play</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">id</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">Number </span><span class="sig-noun">repeatCount</span></span><span class="sig-seperator">)</span></code></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.play</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">id</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">Number </span><span class="sig-noun">repeatCount</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">Number </span><span class="sig-noun">volume</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> id </dt><dd><span class="markdown"><p>A string to refer to sounds</p>
</span></dd></dl><dl class="parameter"><dt> repeatCount </dt><dd><span class="markdown"><p>Repeat count for the file, where -1 stands for repeat forever.</p>
</span></dd></dl><dl class="parameter"><dt> volume </dt><dd><span class="markdown"><p>volume can be a number between 0.0 and 1.0</p>
</span></dd></dl><dl class="parameter returns"><dt class="returns"> [Returns] </dt><dd><span class="markdown"><p>The audio element used to play the sound.  Null if the call failed due to a lack of open channels.</p>
</span></dd></dl></div><span class="markdown"><p>Will play a sound previously added by using the ID that was used in <code>Crafty.audio.add</code>.
Has a default maximum of 5 channels so that the same sound can play simultaneously unless all of the channels are playing.</p>
<p><em>Note that the implementation of HTML5 Audio is buggy at best.</em></p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs ">Crafty.audio.play(<span class="hljs-string">"walk"</span>);

<span class="hljs-comment">//play and repeat forever</span>
Crafty.audio.play(<span class="hljs-string">"backgroundMusic"</span>, <span class="hljs-number">-1</span>);
Crafty.audio.play(<span class="hljs-string">"explosion"</span>,<span class="hljs-number">1</span>,<span class="hljs-number">0.5</span>); <span class="hljs-comment">//play sound once with volume of 50%</span></code></pre></span></div></div><div class="doclet" id="Crafty-audio-remove"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.remove()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L317-L334">src/sound/sound.js#L317-L334</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.remove</span></span><span class="sig-seperator">([</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">id</span></span><span class="sig-seperator">])</span></code><dl class="parameter"><dt> id </dt><dd><span class="markdown"><p>A string to refer to sounds</p>
</span></dd></dl></div><span class="markdown"><p>Will stop the sound and remove all references to the audio object allowing the browser to free the memory.
If no id is given, all sounds will be removed.</p>
<p>This function uses audio path set in Crafty.path in order to remove sound from the assets object.</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs ">Crafty.audio.remove(<span class="hljs-string">"walk"</span>);</code></pre></span></div></div><div class="doclet" id="Crafty-audio-setChannels"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.setChannels()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L270-L277">src/sound/sound.js#L270-L277</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.setChannels</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">Number </span><span class="sig-noun">n</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> n </dt><dd><span class="markdown"><p>The maximum number of channels</p>
</span></dd></dl></div></div><div class="doclet" id="Crafty-audio-stop"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.stop()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L362-L377">src/sound/sound.js#L362-L377</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.stop</span></span><span class="sig-seperator">([</span><span class="sig-phrase"><span class="sig-qualifier">Number </span><span class="sig-noun">ID</span></span><span class="sig-seperator">])</span></code></div><span class="markdown"><p>Stops any playing sound. if id is not set, stop all sounds which are playing</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs "><span class="hljs-comment">//all sounds stopped playing now</span>
Crafty.audio.stop();</code></pre></span></div></div><div class="doclet" id="Crafty-audio-supports"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.supports()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L54-L63">src/sound/sound.js#L54-L63</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.supports</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">extension</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> extension </dt><dd><span class="markdown"><p>A file extension to check audio support for</p>
</span></dd></dl></div><span class="markdown"><p>Return true if the browser thinks it can play the given file type, otherwise false</p>
</span></div><div class="doclet" id="Crafty-audio-toggleMute"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.toggleMute()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L411-L426">src/sound/sound.js#L411-L426</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.toggleMute</span></span><span class="sig-seperator">()</span></code></div><span class="markdown"><p>Mute or unmute every Audio instance that is playing. Toggles between
pausing or playing depending on the state.</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs "><span class="hljs-comment">//toggle mute and unmute depending on current state</span>
Crafty.audio.toggleMute();</code></pre></span></div></div><div class="doclet" id="Crafty-audio-togglePause"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.togglePause()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L525-L540">src/sound/sound.js#L525-L540</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.togglePause</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">string </span><span class="sig-noun">ID</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> {string} id </dt><dd><span class="markdown"><p>The id of the audio object to pause/</p>
</span></dd></dl></div><span class="markdown"><p>Toggle the pause status of the Audio instance specified by id param.</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs ">Crafty.audio.togglePause(<span class="hljs-string">'music'</span>);</code></pre></span></div></div><div class="doclet" id="Crafty-audio-unmute"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.unmute()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L452-L465">src/sound/sound.js#L452-L465</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.unmute</span></span><span class="sig-seperator">()</span></code></div><span class="markdown"><p>Unmute every Audio instance that is playing.</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs ">Crafty.audio.unmute();</code></pre></span></div></div><div class="doclet" id="Crafty-audio-unpause"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.audio.unpause()</h2><div class="doc-source"><a href="https://github.com/craftyjs/Crafty/blob/1ef28454144100272fd439e5c3e8afb90601c00e/src/sound/sound.js#L498-L513">src/sound/sound.js#L498-L513</a></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">Crafty.audio.unpause</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">string </span><span class="sig-noun">ID</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> {string} id </dt><dd><span class="markdown"><p>The id of the audio object to unpause</p>
</span></dd></dl></div><span class="markdown"><p>Resume playing the Audio instance specified by id param.</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs ">Crafty.audio.unpause(<span class="hljs-string">'music'</span>);</code></pre></span></div></div></div></div></div></div>
			
		</div>
	</div>
	<div class="clearer"></div>
	<div id="footer">
		<div id="contact">
			<a href="https://groups.google.com/forum/#!forum/craftyjs">
				<img src="/images/google.png" />
				google groups</a>
			<a href="mailto:starwed@gmail.com">
				<img src="/images/email.png" />
				starwed@gmail.com</a>
			<a href="https://github.com/craftyjs/Crafty">
				<img src="/images/github.png" />
				github</a>
		</div>
		<ul>
			<li><a href="/">Home</a></li>
				<li><a href="/getting-started/">Getting started</a></li>
				<li><a href="/documentation/">Documentation</a></li>
				<li><a href="/api/">API</a></li>
				<li><a href="https://groups.google.com/forum/#!forum/craftyjs">Forum</a></li>
				<li><a href="/components/">Modules</a></li>
				<li class="emph"><a href="/#install">Download</a></li>
		</ul>
		<p>
			&copy; Crafty 2010-2015. Crafty is distributed under the <a href="http://en.wikipedia.org/wiki/MIT_License">MIT License</a>.
		</p>
	</div>
</div>
</body>
</html>
